package hanjia;

import suanfa.ListNode;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/10-下午2:59
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode sentinel = new ListNode();
        sentinel.next = head;
        ListNode temp = head;
        ListNode pre = sentinel;

        while (findNthLast(temp) != n) {
            pre = temp;
            temp = temp.next;
        }
        pre.next = temp.next;
        return sentinel.next;


    }

    public int findNthLast(ListNode listNode) {
        if (listNode == null) {
            return 0;
        }
        return findNthLast(listNode.next) + 1;


    }


}
